home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sndhrdw / berzerk.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  6KB  |  263 lines

  1. /*
  2.  * Berzerk/Frenzy Soundhardware Driver
  3.  * Copyright Alex Judd 1997/98
  4.  * V1.1 for Mame 0.31 13March98
  5.  *
  6.  * these were the original values but I have adjusted them to take account of the
  7.  * default sample rate of 22050. I guess the original samples were at 24000.
  8.  * case 0 : samplefrequency = 19500;
  9.  * case 1 : samplefrequency = 20800;
  10.  * case 2 : samplefrequency = 22320;
  11.  * case 3 : samplefrequency = 24000;
  12.  * case 4 : samplefrequency = 26000;
  13.  * case 5 : samplefrequency = 28400;
  14.  * case 6 : samplefrequency = 31250;
  15.  * case 7 : samplefrequency = 34700;
  16.  *
  17.  */
  18.  
  19. #include "driver.h"
  20. #include <math.h>
  21.  
  22.  
  23.  
  24. #define VOICE_PITCH_MODULATION    /* remove this to disable voice pitch modulation */
  25.  
  26. /* Note: We have the ability to replace the real death sound (33) and the
  27.  * real voices with sample (34) as no good death sample could be obtained.
  28.  */
  29. //#define FAKE_DEATH_SOUND
  30.  
  31.  
  32. /* volume and channel controls */
  33. #define VOICE_VOLUME 100
  34. #define SOUND_VOLUME 80
  35. #define SOUND_CHANNEL 1    /* channel for sound effects */
  36. #define VOICE_CHANNEL 5    /* special channel for voice sounds only */
  37. #define DEATH_CHANNEL 6    /* special channel for fake death sound only */
  38.  
  39. /* berzerk sound */
  40. int lastfreq = 0;
  41. int lastnoise = 0;
  42. int lastvoice = 0;
  43. int lastdata = 0;
  44.  
  45. /* sound controls */
  46. int berzerknoisemulate = 0;
  47. int samplereset = 0;
  48. int voicevolume = 100;
  49. long samplefrequency = 22320;
  50. extern int berzerkplayvoice; /* works in parallel with the read function in machine.c */
  51. extern int collision;
  52. int deathsound = 0;          /* trigger for playing collision sound */
  53. int nextdata5 = -1;
  54.  
  55. int berzerk_sh_start(const struct MachineSound *msound)
  56. {
  57.     int i;
  58.  
  59.  
  60.     berzerknoisemulate = 1;
  61.  
  62.     if (Machine->samples)
  63.     {
  64.         for (i = 0;i < 5;i++)
  65.         {
  66.             if (Machine->samples->sample[i])
  67.                 berzerknoisemulate = 0;
  68.         }
  69.     }
  70.     return 0;
  71. }
  72.  
  73. WRITE_HANDLER( berzerk_sound_control_a_w )
  74. {
  75.     int noise = 0;
  76.     int voice = 0;
  77.     int voicefrequency = 0;
  78.  
  79. /* Throw out all the non-need sound info. (for the moment) */
  80.  
  81.     if (offset < 3) return;
  82.  
  83. /* Decode message for voice samples */
  84.  
  85.     if (offset==4)
  86.     {
  87.         if ((data & 0x40) == 0)
  88.         {
  89.             voice = data;
  90.             berzerkplayvoice = 0;
  91.         }
  92.         else
  93.         {
  94.             /* We should use the volume and frequency on the voice samples */
  95.             voicevolume = ((data & 0x38) >> 3);
  96.             if (voicevolume > 0) voicevolume=255;
  97.  
  98.             voicefrequency = (data & 0x07);
  99. #ifdef VOICE_PITCH_MODULATION
  100.             switch(voicefrequency)
  101.             {
  102.             case 0 : samplefrequency = 17640; break;
  103.             case 1 : samplefrequency = 19404; break;
  104.             case 2 : samplefrequency = 20947; break;
  105.             case 3 : samplefrequency = 22050; break;
  106.             case 4 : samplefrequency = 26019; break;
  107.             case 5 : samplefrequency = 27783; break;
  108.             case 6 : samplefrequency = 31250; break;
  109.             case 7 : samplefrequency = 34700; break;
  110.             default: samplefrequency = 22050; break;
  111.             }
  112. #endif
  113.             return;
  114.         }
  115.     }
  116.  
  117. /* Check for player fire sample reset */
  118.  
  119.     if ((offset==3) || (offset==5))
  120.     {
  121.         if (lastnoise==70)
  122.         {
  123.             if ((offset==3) && (data==172))
  124.             {
  125.                 nextdata5 = 25;
  126.                 return;
  127.             }
  128.  
  129.             if (offset==5)
  130.             {
  131.                 if (data==nextdata5)
  132.                 {
  133. #ifdef FAKE_DEATH_SOUND
  134.                     deathsound = 1; /* trigger death sound */
  135. #else
  136.                     deathsound = 2; /* trigger death sound */
  137. #endif
  138.                     lastnoise = 64; /* reset death sound */
  139.                 }
  140.  
  141.                 nextdata5 = -1;
  142.             }
  143.  
  144.             return;
  145.         }
  146.  
  147.         if (lastnoise==69)
  148.         {
  149.             if ((offset==3) && (data==50))
  150.             {
  151.                 nextdata5 = 50;
  152.                 return;
  153.             }
  154.  
  155.             if (offset==5)
  156.             {
  157.                 if (data==nextdata5)
  158.                 {
  159.                     lastnoise = 64; /* reset laser sound */
  160.                 }
  161.  
  162.                 nextdata5 = -1;
  163.             }
  164.         }
  165.  
  166.         return;
  167.     }
  168.  
  169. /* Check to see what game sound should be playing */
  170.  
  171.     if ((data > 60) && (data < 72) && (offset==6))
  172.         noise = data;
  173.     else
  174.         noise = lastnoise;
  175.  
  176. /* One day I'll do this... */
  177.  
  178.     if (berzerknoisemulate)
  179.     {
  180.         return;
  181.         if (voicefrequency != 1750*(4-noise))
  182.         {
  183.             voicefrequency = 1750*(4-noise);
  184.             voicevolume = 85*noise;
  185.         }
  186.  
  187.         if (noise)
  188.         {
  189.             sample_set_freq(2,voicefrequency);
  190.             sample_set_volume(2,voicevolume);
  191.         }
  192.         else
  193.         {
  194.             sample_set_volume(2,0);
  195.             voicevolume = 0;
  196.         }
  197.     }
  198.     else
  199.     {
  200.         if ((offset==6) && (lastnoise != noise))  /* Game sound effects */
  201.         {
  202.             switch (noise)
  203.             {
  204.             case 69 : /* shot sample */
  205.                 sample_start(1,30,0);
  206.                 break;
  207.             case 70 : /* baddie laser */
  208. logerror("Trying death sound");
  209.                 switch(deathsound)
  210.                 {
  211.                 case 1 :
  212.                     sample_start(2,33,0);
  213.                     deathsound=0;
  214.                     break;
  215.                 case 2 :
  216.                     sample_start(DEATH_CHANNEL,34,0);
  217.                     deathsound=3;
  218.                     break;
  219.                 case 0 :
  220.                     sample_start(2,31,0);
  221.                     break;
  222.                 }
  223.                 break;
  224.             case 71 : /* kill baddie */
  225.                 sample_start(3,32,0);
  226.                 break;
  227.             default :
  228.                 break;
  229.             }
  230.         }
  231.         lastnoise = noise;           /* make sure we only play the sound once */
  232.  
  233.         if (offset==4)               /* Game voice sounds */
  234.         {
  235.             if (deathsound<2)           /* no voices for real death sound */
  236.             {
  237.                 if (lastvoice==24 && voice==27)
  238.                 {
  239.                     lastvoice=voice;    /* catch for the 'chicken ayye' problem */
  240.                     return;
  241.                 }
  242.                 sample_start(VOICE_CHANNEL,voice,0);
  243. #ifdef VOICE_PITCH_MODULATION
  244.                 sample_set_freq(VOICE_CHANNEL,samplefrequency);
  245. #endif
  246.                 lastvoice=voice;
  247.             }
  248.         }
  249.     } /* End of berzerknoisemulate */
  250. }
  251.  
  252. WRITE_HANDLER( berzerk_sound_control_b_w )
  253. {
  254.     logerror("B Data value %d and offset %d at %d\n", data, offset, lastfreq);
  255. }
  256.  
  257. void berzerk_sh_update(void)
  258. {
  259.     berzerkplayvoice = !sample_playing(VOICE_CHANNEL);
  260.     if (deathsound==3 && sample_playing(DEATH_CHANNEL) == 0 && lastnoise != 70)
  261.         deathsound=0;               /* reset death sound */
  262. }
  263.